from fastapi import APIRouter,Depends
from fastapi.responses import StreamingResponse
from sse_starlette import EventSourceResponse
import asyncio

from app.utils.custom_route import CustomRoute
from app.utils.auth import get_current_user

from app.schema.message import *
from app.schema.base import *
from app.schema.base import RespBaseSchema,RespDataSchema
from app.service.message import MessageService
from app.dao.message import MessageDao
from app.utils.decorate import decorate

router = APIRouter(route_class=CustomRoute)


@router.post('/messagelist')
async def get_message(*, args: ListArgsSchema, auth_data: dict = Depends(get_current_user)):
   try:
       message=MessageService(auth_data).get_message_list(args)
       message.list.reverse()
       return RespDataSchema(data={"list":message.list, "page_count":message.page_count})
   except BaseException as e:
       return  RespBaseSchema(code=50001,message='获取失败')

@router.post('/sendonemessage')
async def send_one_message(message: MessageInfoSchema, auth_data: dict = Depends(get_current_user)):
   try:
       message.user_id=auth_data.id
       message.user_type = 0
       return EventSourceResponse(decorate(MessageService(auth_data).send_one_message(message)))
   except BaseException as e:
       return  RespBaseSchema(code=50001,message='发送失败')
   
@router.post('/stream')
async def send_one_message(message: MessageInfoSchema):
   try:
       return EventSourceResponse(decorate(MessageService(MessageCreateSchema()).stream(message)))
   except BaseException as e:
       return  RespBaseSchema(code=50001,message='发送失败')
   
@router.post('/nostream')
async def response_no_stream(message: MessageInfoSchema):
   try:
       return MessageService(MessageCreateSchema()).nostream(message=message)
   except BaseException as e:
       return  RespBaseSchema(code=50001,message='发送失败')